package cc.vv.baselibrary.util.download;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.ResultReceiver;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.IOException;

import cc.vv.baselibrary.BuildConfig;
import cc.vv.baselibrary.util.LKLogUtil;
import cc.vv.baselibrary.view.JniCancelAndConfirmDialog;
import cc.vv.baselibrary.view.JniDownloadProgressBarDialog;


/**
 * 下载应用apk包的Dialog
 * Created by sun.li on 2017/10/18.
 *
 * @author sun.li
 */

public class DownloadApkUtil {

    private static final String TAG = "UpdateChecker";
    private Context mContext;
    /**
     * 版本对比地址
     */
    private String mCheckUrl;
    private ApkDownloadInfoObj mAppVersion;

    /**
     * 下载apk的对话框
     */
    private JniDownloadProgressBarDialog jniDownloadProgressBarDialog;

    private File apkFile;

    private OnButtonClick onButtonClick;

    /**
     * 下载提示框Dialog对象
     */
    private JniCancelAndConfirmDialog jniCancelAndConfirmDialog;

    /**
     * 版本更新标志，有更新"Yes"
     */
    public static final String UpdateMarkYes = "Yes";

    public void setCheckUrl(String url) {
        mCheckUrl = url;
    }

    public DownloadApkUtil(Context context, ApkDownloadInfoObj mAppVersion) {
        this.mAppVersion = mAppVersion;
        this.mContext = context;
        // instantiate it within the onCreate method
        initDownloagDialog();
    }

    public void initDownloagDialog() {
        if (jniDownloadProgressBarDialog == null) {
            jniDownloadProgressBarDialog = new JniDownloadProgressBarDialog(mContext);
        }
        jniDownloadProgressBarDialog.setTitle("正在下载");
        jniDownloadProgressBarDialog.setCancelable(true);
        jniDownloadProgressBarDialog.setEventInterface(new JniDownloadProgressBarDialog.EventInterface() {
            @Override
            public void cancelOnClick() {

            }
        });
    }

    /**
     * 显示更新提示的Dialog
     */
    public void showUpdateDialog() {
        if (jniCancelAndConfirmDialog == null) {
            jniCancelAndConfirmDialog = new JniCancelAndConfirmDialog(mContext);
        }
        jniCancelAndConfirmDialog.setTitle("有新版本");
        jniCancelAndConfirmDialog.setCancelable(true);
        jniCancelAndConfirmDialog.setContent(mAppVersion.getInfo());
        jniCancelAndConfirmDialog.setConfirmBtnText("下载");
        if ("1".equals(mAppVersion.getForcedUpdatingState())) {
            jniCancelAndConfirmDialog.setCancelBtnText("");
        } else {
            jniCancelAndConfirmDialog.setCancelBtnText("忽略");
        }
        jniCancelAndConfirmDialog.setEventInterface(new JniCancelAndConfirmDialog.EventInterface() {
            @Override
            public void cancelOnClick() {
                if (onButtonClick != null) {
                    onButtonClick.negativeClick();
                }
                if (jniCancelAndConfirmDialog != null) {
                    jniCancelAndConfirmDialog.dismiss();
                }
            }

            @Override
            public void confirmOnClick() {
                if (onButtonClick != null) {
                    onButtonClick.positiveClick();
                }
                downLoadApk();
                if (jniCancelAndConfirmDialog != null) {
                    jniCancelAndConfirmDialog.dismiss();
                }
            }
        });
        jniCancelAndConfirmDialog.show();
    }

    public void downLoadApk() {
        if (mAppVersion == null || TextUtils.isEmpty(mAppVersion.getUrl())) {
            if (onButtonClick != null) {
                onButtonClick.negativeClick();
            }
            return;
        }
        jniDownloadProgressBarDialog.setProgressBar(0);
        jniDownloadProgressBarDialog.show();
        String apkUrl = mAppVersion.getUrl();
        String dir = Environment.getExternalStorageDirectory().getPath() + "/tech.yunjing.biconlife/files/apk";
        LKLogUtil.e("dir:" + dir);
        DownloadFileUtil.createOrExistsDir(dir);
        File folder = new File(dir);
        if (folder.exists() && folder.isDirectory()) {
            //do nothing
        } else {
            folder.mkdirs();
        }
        if (TextUtils.isEmpty(apkUrl)) {
            return;
        }

        if (apkUrl.contains("/")) {
            String filename = apkUrl.substring(apkUrl.lastIndexOf("/"), apkUrl.length());
            String destinationFilePath = dir + filename;
            File file = new File(dir, filename);
            try {
                if (!file.exists()) {
                    file.createNewFile();
                }
            } catch (IOException e) {
                LKLogUtil.e(e.getMessage(), e);
            }

            apkFile = new File(destinationFilePath);

            Intent intent = new Intent(mContext, DownloadApkService.class);
            intent.putExtra("url", apkUrl);
            intent.putExtra("dest", destinationFilePath);
            intent.putExtra("receiver", new DownloadReceiver(new Handler()));
            mContext.startService(intent);
        }
    }

    private class DownloadReceiver extends ResultReceiver {
        public DownloadReceiver(Handler handler) {
            super(handler);
        }

        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            super.onReceiveResult(resultCode, resultData);
            if (resultCode == DownloadApkService.UPDATE_PROGRESS) {
                int progress = 0;
                try {
                    progress = resultData.getInt("progress");
                } catch (Exception e) {
                    LKLogUtil.e(e.getMessage(), e);
                    progress = -1;
                }
                if (progress <= DownloadApkService.HttpURLConnectionDownloadCompleteCode && progress >= 0) {
                    jniDownloadProgressBarDialog.setProgressBar(progress);
                }
                if (progress == DownloadApkService.HttpURLConnectionDownloadCompleteCode) {
                    /* 更新下载链接正常连通*/
                    jniDownloadProgressBarDialog.dismiss();
                    LKLogUtil.e("apkFile:" + apkFile.toString());
                    //如果没有设置SDCard写权限，或者没有sdcard,apk文件保存在内存中，需要授予权限才能安装
                    String[] command = {"chmod", "777", apkFile.toString()};
                    try {
//                        ProcessBuilder builder = new ProcessBuilder(command);
//                        builder.start();

                        installApk(apkFile);
                    } catch (Exception e) {
                        if (onButtonClick != null) {
                            onButtonClick.negativeClick();
                        }
                    }
                } else if (progress == DownloadApkService.HttpURLConnectionErrorCode) {
                    /* 进度异常时回调*/
                    jniDownloadProgressBarDialog.dismiss();
                    if (jniCancelAndConfirmDialog == null) {
                        jniCancelAndConfirmDialog = new JniCancelAndConfirmDialog(mContext);
                    }
                    jniCancelAndConfirmDialog.setTitle("版本更新异常");
                    jniCancelAndConfirmDialog.setCancelable(false);
                    jniCancelAndConfirmDialog.setContent(mAppVersion.getInfo());
                    jniCancelAndConfirmDialog.setConfirmBtnText("重新下载");
                    jniCancelAndConfirmDialog.setCancelBtnText("取消");
                    jniCancelAndConfirmDialog.setEventInterface(new JniCancelAndConfirmDialog.EventInterface() {
                        @Override
                        public void cancelOnClick() {
                            if (onButtonClick != null) {
                                onButtonClick.negativeClick();
                            }
                            if (jniCancelAndConfirmDialog != null) {
                                jniCancelAndConfirmDialog.dismiss();
                            }
                        }

                        @Override
                        public void confirmOnClick() {
                            if (onButtonClick != null) {
                                onButtonClick.positiveClick();
                            }
                            downLoadApk();
                            if (jniCancelAndConfirmDialog != null) {
                                jniCancelAndConfirmDialog.dismiss();
                            }
                        }
                    });
                    jniCancelAndConfirmDialog.show();
                }
            }
        }
    }

    /**
     * 安装应用
     *
     * @param file
     */
    private void installApk(File file) {

        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri apkUri = null;
        //判断版本是否是 7.0 及 7.0 以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            apkUri = FileProvider.getUriForFile(mContext, "cc.vv.baselibrary.fileprovider", apkFile);
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            apkUri = Uri.fromFile(apkFile);
        }
        intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        intent.setAction(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
//        android.os.Process.killProcess(android.os.Process.myPid());
    }


    public OnButtonClick getOnButtonClick() {
        return onButtonClick;
    }

    public void setOnButtonClick(OnButtonClick onButtonClick) {
        this.onButtonClick = onButtonClick;
    }

    /**
     * Button点击回调
     */
    public interface OnButtonClick {
        /**
         * 确认按钮点击
         */
        void positiveClick();

        /**
         * 取消按钮点击
         */
        void negativeClick();
    }

}
